﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using DataViews.Messages;
using DataViews.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataViews.ViewModels
{
    public partial class FilterListViewModel<T> : ObservableObject,IContext
    {
        [ObservableProperty]
        ObservableCollection<FilterItem> filters = new ObservableCollection<FilterItem>();

        [ObservableProperty]
        bool allFiltersSelected = true;       

        public FilterListViewModel(string context, ObservableCollection<FilterItem> filters)
        {
            Context = context;
            Filters = filters;

            this.PropertyChanged += ((o, e) => {
                if (e.PropertyName == "AllFiltersSelected")
                {
                    foreach (var filter in Filters)
                    {
                        filter.Selected = AllFiltersSelected;
                    }
                }

            });
        }

        public string Context { get; set; }

        [RelayCommand]
        void DoFilter()
        {

            WeakReferenceMessenger.Default.Send<DoFilterMessage>(new DoFilterMessage(Context,Filters));
        }
    }
}
